package com.example.day2arouter;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.day2arouter.entity.UserInfo;
import com.example.day2arouter.view.Pig1Fragment;

import static com.example.day2arouter.view.Path.DATA;
import static com.example.day2arouter.view.Path.Frag;
import static com.example.day2arouter.view.Path.INTERCEPTOR;
import static com.example.day2arouter.view.Path.Tiao;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button bt1;
    private Button bt2;
    private Button bt3;
    private Button bt4;
    private Button bt5;
    private Button bt6;
    private FrameLayout fl;
    private Handler handler=new Handler();
    Fragment fragment = (Fragment) ARouter.getInstance().build(Frag).navigation();

    private final int REQUEST_CODE_TIAO=0x1001;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView() {

        bt1 = (Button) findViewById(R.id.bt1);
        bt2 = (Button) findViewById(R.id.bt2);
        bt3 = (Button) findViewById(R.id.bt3);
        bt4 = (Button) findViewById(R.id.bt4);
        bt5 = (Button) findViewById(R.id.bt5);
        bt6 = (Button) findViewById(R.id.bt6);
        fl = (FrameLayout) findViewById(R.id.fl);
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
        bt5.setOnClickListener(this);
        bt6.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt1:  //应用内跳转
                ARouter.getInstance().build(Tiao).navigation(this,REQUEST_CODE_TIAO);
                break;
            case R.id.bt2:   //基础数据传递
                ARouter.getInstance().build(DATA)
                        .withString("name","涂山苏苏")
                        .withInt("age",500)
                        .navigation();
                break;
            case R.id.bt3:  //传对象
                UserInfo userInfo = new UserInfo("东方月初", 20);
                ARouter.getInstance().build(DATA).withObject("info",userInfo).navigation();
                break;
            case R.id.bt4:   //引入fragment
               if(!fragment.isAdded()){
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.fl,fragment).commit();
                }
                break;
            case R.id.bt5:   //组件间跳转
                ARouter.getInstance().build("/mylibrary_modules/MainActivity").navigation();

                break;
            case R.id.bt6:   //拦截
                ARouter.getInstance().build(INTERCEPTOR).navigation(this, new NavigationCallback() {
                    @Override
                    public void onFound(Postcard postcard) {

                    }

                    @Override
                    public void onLost(Postcard postcard) {

                    }

                    @Override
                    public void onArrival(Postcard postcard) {

                    }

                    @Override
                    public void onInterrupt(Postcard postcard) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "您被打劫啦啦啦啦啦啦啦", Toast.LENGTH_SHORT).show();

                            }
                        });
                    }
                });

                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case REQUEST_CODE_TIAO:
                if(resultCode==RESULT_OK){
                    finish();
                }
                break;
        }
    }
}
